﻿Public Class ClsTGeneralQuantityLedgerDbAccess
    Inherits ClsBaseDbAccess

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_T_GENERAL_QUANTITY_LEDGER)
    End Sub

    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub

    ''' <summary>
    ''' 清空指定期间的数据（除年初数据外全部置0）
    ''' </summary>
    ''' <param name="wp"></param>
    ''' <returns></returns>
    Public Function ClearDataByWorkPeriod(ByVal wp As String) As Integer

        Dim tmpRetInt As Integer

        Try

            Dim tmpSql As String
            Dim tmpParas(18) As OleDb.OleDbParameter

            Dim tmpDbAccess = GetConnection()

            '打开数据库
            tmpDbAccess.DbOpen()

            '构造查询语句
            tmpSql = "Update [" & Me.AppTableName & "] set "
            tmpSql &= "[BeginBalance] = @BeginBalance,[BeginPrice] = @BeginPrice,[BeginMoney] = @BeginMoney,"
            tmpSql &= "[CreditSum] = @CreditSum,[CreditPrice] = @CreditPrice,[CreditSumMoney] = @CreditSumMoney,"
            tmpSql &= "[CreditTotal] = @CreditTotal,[CreditTotalPrice] = @CreditTotalPrice,[CreditTotalMoney] = @CreditTotalMoney,"
            tmpSql &= "[DebitSum] = @DebitSum,[DebitSumPrice] = @DebitSumPrice,[DebitSumMoney] = @DebitSumMoney,"
            tmpSql &= "[DebitTotal] = @DebitTotal,[DebitTotalPrice] = @DebitTotalPrice,[DebitTotalMoney] = @DebitTotalMoney,"
            tmpSql &= "[Balance] = @Balance,[BalancePrice] = @BalancePrice,[BalanceMoney] = @BalanceMoney"
            tmpSql &= " where [WorkPeriod]=@WorkPeriod "

            tmpParas(0) = New OleDb.OleDbParameter("@BeginBalance", OleDb.OleDbType.Decimal)
            tmpParas(1) = New OleDb.OleDbParameter("@CreditSum", OleDb.OleDbType.Decimal)
            tmpParas(2) = New OleDb.OleDbParameter("@CreditTotal", OleDb.OleDbType.Decimal)
            tmpParas(3) = New OleDb.OleDbParameter("@DebitSum", OleDb.OleDbType.Decimal)
            tmpParas(4) = New OleDb.OleDbParameter("@DebitTotal", OleDb.OleDbType.Decimal)
            tmpParas(5) = New OleDb.OleDbParameter("@Balance", OleDb.OleDbType.Decimal)
            tmpParas(6) = New OleDb.OleDbParameter("@Balance", OleDb.OleDbType.Decimal)
            tmpParas(7) = New OleDb.OleDbParameter("@BeginBalance", OleDb.OleDbType.Decimal)
            tmpParas(8) = New OleDb.OleDbParameter("@CreditSum", OleDb.OleDbType.Decimal)
            tmpParas(9) = New OleDb.OleDbParameter("@CreditTotal", OleDb.OleDbType.Decimal)
            tmpParas(10) = New OleDb.OleDbParameter("@DebitSum", OleDb.OleDbType.Decimal)
            tmpParas(11) = New OleDb.OleDbParameter("@DebitTotal", OleDb.OleDbType.Decimal)
            tmpParas(12) = New OleDb.OleDbParameter("@Balance", OleDb.OleDbType.Decimal)
            tmpParas(13) = New OleDb.OleDbParameter("@Balance", OleDb.OleDbType.Decimal)
            tmpParas(14) = New OleDb.OleDbParameter("@BeginBalance", OleDb.OleDbType.Decimal)
            tmpParas(15) = New OleDb.OleDbParameter("@CreditSum", OleDb.OleDbType.Decimal)
            tmpParas(16) = New OleDb.OleDbParameter("@CreditTotal", OleDb.OleDbType.Decimal)
            tmpParas(17) = New OleDb.OleDbParameter("@DebitSum", OleDb.OleDbType.Decimal)
            tmpParas(18) = New OleDb.OleDbParameter("@WorkPeriod", OleDb.OleDbType.VarWChar)

            tmpParas(0).Value = 0
            tmpParas(1).Value = 0
            tmpParas(2).Value = 0
            tmpParas(3).Value = 0
            tmpParas(4).Value = 0
            tmpParas(5).Value = 0
            tmpParas(6).Value = 0
            tmpParas(7).Value = 0
            tmpParas(8).Value = 0
            tmpParas(9).Value = 0
            tmpParas(10).Value = 0
            tmpParas(11).Value = 0
            tmpParas(12).Value = 0
            tmpParas(13).Value = 0
            tmpParas(14).Value = 0
            tmpParas(15).Value = 0
            tmpParas(16).Value = 0
            tmpParas(17).Value = 0
            tmpParas(18).Value = wp

            tmpRetInt = tmpDbAccess.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDbAccess.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTGeneralQuantityLedger - ClearDataByWorkPeriod"

        End Try

        Return tmpRetInt

    End Function


    Public Overrides Function ConvertRecordToClass(reader As OleDb.OleDbDataReader) As Object

        If reader Is Nothing Then
            Return Nothing
        End If

        Dim tmpList As List(Of ClsTGeneralQuantityLedger) = New List(Of ClsTGeneralQuantityLedger)

        Try

            Dim tmpCls As ClsTGeneralQuantityLedger

            While reader.Read()

                tmpCls = New ClsTGeneralQuantityLedger()

                With tmpCls

                    .GeneralId = ConvertDbNullInteger(reader(0))
                    .WorkPeriod = ConvertDbNullString(reader(1))
                    .AcctCode = ConvertDbNullString(reader(2))
                    .AcctLevel = ConvertDbNullInteger(reader(3))
                    .IfLastLevel = reader.GetBoolean(4)
                    .AcctName = ConvertDbNullString(reader(5))
                    .Year = ConvertDbNullSingle(reader(6))
                    .YearPrice = ConvertDbNullDecimal(reader(7))
                    .YearMoney = ConvertDbNullDecimal(reader(8))
                    .BeginBalance = ConvertDbNullSingle(reader(9))
                    .BeginPrice = ConvertDbNullDecimal(reader(10))
                    .BeginMoney = ConvertDbNullDecimal(reader(11))
                    .CreditSum = ConvertDbNullSingle(reader(12))
                    .CreditPrice = ConvertDbNullDecimal(reader(13))
                    .CreditSumMoney = ConvertDbNullDecimal(reader(14))
                    .CreditTotal = ConvertDbNullSingle(reader(15))
                    .CreditTotalPrice = ConvertDbNullDecimal(reader(16))
                    .CreditTotalMoney = ConvertDbNullDecimal(reader(17))
                    .DebitSum = ConvertDbNullSingle(reader(18))
                    .DebitSumPrice = ConvertDbNullDecimal(reader(19))
                    .DebitSumMoney = ConvertDbNullDecimal(reader(20))
                    .DebitTotal = ConvertDbNullSingle(reader(21))
                    .DebitTotalPrice = ConvertDbNullDecimal(reader(22))
                    .DebitTotalMoney = ConvertDbNullDecimal(reader(23))
                    .Balance = ConvertDbNullSingle(reader(24))
                    .BalancePrice = ConvertDbNullDecimal(reader(25))
                    .BalanceMoney = ConvertDbNullDecimal(reader(26))
                    .BalanceDirection = ConvertDbNullString(reader(27))

                End With

                tmpList.Add(tmpCls)

            End While

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTGeneralMoneyLedgerDbAccess - ConvertRecordToClass"

        End Try

        Return tmpList

    End Function
End Class
